package com.jalarbee.stock.sales.business;

import com.jalarbee.core.business.PricedEntity;
import com.jalarbee.core.money.MonetaryAmount;
import com.jalarbee.core.user.business.User;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Abdoulaye <b>Ikhnaton</b> Diallo
 */
@Entity
public class Sale extends PricedEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "SALE_DATE")
    private Date date;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "sale")
    private List<LineItem> lineItems;
    @ManyToOne(optional = true)
    private User customer;
    @ManyToOne(optional = false)
    private User salesperson;
    @Column(nullable=true)
    private String name;
    @Column(nullable=true)
    private String description;
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Column(name="LAST_UPDATE_TIME")
    private Date lastUpdateTime;
    @Column(name="ITEM_COUNT")
    private int itemCount;

    public Sale() {
    }

    
    @PrePersist
    @PreUpdate
    private void handleEvents() {
        correctPrices();
        correctLastUpdateTime();
        correctItemCount();
    }
    
    
    
    private void correctItemCount() {
        int count = 0;
        for (LineItem item : lineItems) {
            count += item.getQuantity().getCount();
        }
        setItemCount(count);
    }
    
    private void correctLastUpdateTime() {
        setLastUpdateTime(new Date());
    }
    private void correctPrices() {
        MonetaryAmount maRetailPrice = new MonetaryAmount();
        MonetaryAmount maStockPrice = new MonetaryAmount();
        MonetaryAmount maCreditPrice = new MonetaryAmount();
        for (LineItem item : lineItems) {
            maRetailPrice.add(item.getRetailPrice());
            maCreditPrice.add(item.getCreditPrice());
            maStockPrice.add(item.getStockPrice());
        }
        setRetailPrice(maRetailPrice);
        setStockPrice(maStockPrice);
        setCreditPrice(maCreditPrice);
    }

    public User getCustomer() {
        return customer;
    }

    public void setCustomer(User customer) {
        this.customer = customer;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public User getSalesperson() {
        return salesperson;
    }

    public void setSalesperson(User salesperson) {
        this.salesperson = salesperson;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public int getItemCount() {
        return itemCount;
    }

    public void setItemCount(int itemCount) {
        this.itemCount = itemCount;
    }

    
    
}
